<?php

/**
 * Created by Zhuma
 * User: 
 * Date: 2016-08-1
 * Time: 09:46
 */
/*
 * 工具类
 */

namespace Home\Model;
use Think\ITime;

class Tool {

    /**
     * 获取用户真实 IP
     */
    public static function getIP() {
        static $realip = null;//客户IP
        if (isset($_SERVER)) {
            if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {//判断服务器环境变量是否有客户的IP
                $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
            } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {//判断服务器环境变量是否有客户的IP
                $realip = $_SERVER["HTTP_CLIENT_IP"];
            } else {//判断服务器环境变量是否有客户的IP
                $realip = $_SERVER["REMOTE_ADDR"];
            }
        } else {
            /*geteven 获取环境变量的值
            HTTP_CLIENT_IP:客户端的ip 
            HTTP_X_FORWARDED_FOR:浏览当前页面的用户计算机的网关  REMOTE_ADDR:浏览当前页面的用户计算机的ip地址*/
            if (getenv("HTTP_X_FORWARDED_FOR")) {
                $realip = getenv("HTTP_X_FORWARDED_FOR");
            } else if (getenv("HTTP_CLIENT_IP")) {
                $realip = getenv("HTTP_CLIENT_IP");
            } else {
                $realip = getenv("REMOTE_ADDR");
            }
        }
        return $realip;
    }


    /*
     * 获取网络类型
     */
    public static function getNetType($ua = '') {
 
        $ua or $ua = $_SERVER['HTTP_USER_AGENT'];
        if (stripos($ua, 'NetType')) {
            preg_match('/NetType\/(\d\S|\S{4,})/i', $ua, $match);
            return $match[1];
        } else {
            return '未知';
        }
        
    }


    /*
     * 获取设备操作系统及版本
     */
    public static function getSystem($ua = '') {
 
        $ua or $ua = $_SERVER['HTTP_USER_AGENT'];

        if (isset($ua)) {
            if (strpos($ua, 'Android')) {
                preg_match("/(?<=Android )[\d\.]{1,}/", $ua, $version);
                preg_match('/(.*)(?<=; ).+?(?= Build)/i', $ua, $model);
                $arr['model'] = str_replace($model[1],"",$model[0]);
                $arr['system'] = 'Android ' . $version[0];
            } elseif (strpos($ua, 'iPhone')) {
                preg_match("/(?<=CPU iPhone OS )[\d\_]{1,}/", $ua, $version);
                $arr['model'] = 'iPhone';
                $arr['system'] = 'iPhone ' . str_replace('_', '.', $version[0]);
            } elseif (strpos($ua, 'iPad')) {
                preg_match("/(?<=CPU OS )[\d\_]{1,}/", $ua, $version);
                $arr['system'] = 'iPad ' . str_replace('_', '.', $version[0]);
            }  elseif (strpos($ua, 'Windows')) {
                preg_match("/(?<=Windows NT )[\d\.]{1,}/", $ua, $version);
                $arr['system'] = 'Windows NT ' . $version[0];
            }
            return $arr;
        } else {
            return FALSE;
        }
    }
   /**
     * 获取客户端游览器
     * @params $ua  User-Agent
     * @author haha 2016-01-21
     */
    public static function getClientBroswer($ua = '') {
 
        $ua or $ua = $_SERVER['HTTP_USER_AGENT'];
 
        //微信打开
        if (stripos($ua, 'MicroMessenger')) {
            preg_match('/MicroMessenger\/([\d\.]+)/i', $ua, $match);
            return 'Wechat '. $match[1];
        }
        
        // QQ打开
        if (stripos($ua, 'QQ')) {
            preg_match('/QQ\/([\d\.]+)/i', $ua, $match);
            return 'QQ '. $match[1];
        }
 
        //获取火狐浏览器的版本号
        if (stripos($ua, 'Firefox/')) {
            preg_match('/Firefox\/([^;)]+)/i', $ua, $match);
            $exp[0] = 'Firefox';
            $exp[1] = $match[1];
        }
        //360游览器
        else if(stripos($ua,'360SE')) {
            $exp[0] = '360游览器';
            $exp[1] = '';
        }
        //搜狗游览器
        else if(stripos($ua, 'SE') && stripos($ua, 'MetaSr')) {
            preg_match('/SE\s([\w\.]+)/i', $ua, $match);
            $exp[0] = '搜狗';
            $exp[1] = $match[1];
        }
        //获取google chrome的版本号
        else if (stripos($ua, 'Chrome')) {
            preg_match('/Chrome\/([\d\.]+)/', $ua, $match);
            $exp[0] = 'Chrome';
            $exp[1] = $match[1];
        }
        //遨游游览器
        elseif (stripos($ua, 'Maxthon')) {
            preg_match('/Maxthon([\s\d\.]+)/', $ua, $match);
            $exp[0] = '傲游';
            $exp[1] = $match[1];
        }
        //欧朋游览器
        elseif (stripos($ua, 'opera')) {
            preg_match('/opera\/([\d\.\/]+)/', $ua, $match);
            $exp[0] = 'Opera';
            $exp[1] = $match[1];
        }
        //win10 Edge浏览器 添加了chrome内核标记 在判断Chrome之前匹配
        else if(stripos($ua, 'Edge')) {
            preg_match('/Edge\/([\d\.]+)/', $ua, $match);
            $exp[0] = 'Edge';
            $exp[1] = $match[1];
        }
        //UC游览器
        else if(stripos($ua,'UCBrowser')) {
            preg_match('/UCBrowser\/([\d\.]+)/', $ua, $match);
            $exp[0] = 'UCBrowser ';
            $exp[1] = $match[1];
        }
        //QQ游览器（note:移动端没有检测版本号）
        else if(stripos($ua,'TencentTraveler') || stripos($ua,'QQBrowser')) {
            preg_match('/TencentTraveler\s([\d\.]+)/', $ua, $match);
            $exp[0] = 'QQ游览器';
            $exp[1] = $match[1];
        }
        //百度游览器
        else if(stripos($ua,'baidubrowser')) {
            preg_match('/baidubrowser\/([\d\.]+)/', $ua, $match);
            $exp[0] = '百度游览器';
            $exp[1] = $match[1];
        }
        //IE11
        else if(stripos($ua,'rv:')) {
            preg_match('/rv:([\d\.]+)/', $ua, $match);
            $exp[0] = 'IE';
            $exp[1] = $match[1];
        }
        //移动设备的IE游览器
        else if(stripos($ua, 'IEMobile')) {
            preg_match('/IEMobile\/([\d\.]+)+/i', $ua, $match);
            $exp[0] = 'IE';
            $exp[1] = $match[1];
        }
        //获取IE的版本号
        else if (stripos($ua, 'MSIE')) {
            preg_match('/MSIE\s+([^;)]+)+/i', $ua, $match);
            $exp[0] = 'IE';
            $exp[1] = $match[1];
        }
        else {
            $exp[0] = '未知浏览器';
            $exp[1] = '';
        }
        return implode(' ',$exp);
    }
    
    /**
     * 页面日志 
     */
    public static function log(){
        $logDB = M('user_log');
        $data = array(
            'mes' =>  $_SERVER['HTTP_USER_AGENT'],
            'time'=> ITime::getTime,
            'cookie' => $_SERVER['HTTP_COOKIE']
        );
    }
 
    
  function getMAC() {
        @exec("ipconfig /all", $array);
        dump($array);dei;
        for ($Tmpa; $Tmpa < count($array); $Tmpa++) {
            if (eregi("Physical", $array[$Tmpa])) {
                $mac = explode(":", $array[$Tmpa]);
                return $mac[1];
            }
        }
    }
}